//
//  MainViewController.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 19/04/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.tintColor = UIColor.orange
        addChildViewController(childrenVC: "HomeViewController",title: "Home",imageName: "tabbar_home")
        addChildViewController(childrenVC: "MessageViewController",title: "Message",imageName: "tabbar_message_center")
        addChildViewController(childrenVC: "MiddleViewController", title: "", imageName: "")
        addChildViewController(childrenVC: "FindViewController",title: "Find",imageName: "tabbar_discover")
        addChildViewController(childrenVC: "MyViewController",title: "My",imageName: "tabbar_profile")

    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

    }
    override func viewDidLayoutSubviews() {
        setUpComposeBtn()
    }
    private func setUpComposeBtn()
    {
        // 添加加号按钮
        tabBar.addSubview(composeBtn)
        // 调整位置
        let width = UIScreen.main.bounds.width / CGFloat((viewControllers?.count)!)
        let height = tabBar.bounds.height-SAFE_SCREEN_BOTTOM
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        composeBtn.frame = rect.offsetBy(dx: width*2, dy: 0)
    }

//    @objc private func addChildViewController(childrenVC:UIViewController,title:String,imageName:String) -> Void {
//            // 1.1 设置首页tabbar对应的数据
//            childrenVC.tabBarItem.image = UIImage(named: imageName)
//            childrenVC.tabBarItem.selectedImage = UIImage(named:imageName+"_highlighted")
//            childrenVC.title = title
//
//            // 给首页包装一个导航控制器
//            let navi = UINavigationController()
//            navi.addChildViewController(childrenVC)
//
//            // 将导航控制器添加到tabbarVC
//        addChildViewController(navi)
//      }
    @objc private func addChildViewController(childrenVC:String,title:String,imageName:String) -> Void {
        let nameString = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        let className:AnyClass? = NSClassFromString(nameString+"."+childrenVC)
        // 通过类创建对象
        let vcClass = className as! UIViewController.Type
        let vc = vcClass.init()
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named:imageName+"_highlighted")
        vc.title = title

        // 给首页包装一个导航控制器
        let navi = UINavigationController()
        navi.addChildViewController(vc)

        // 将导航控制器添加到tabbarVC
        addChildViewController(navi)

        print(vc)
        
    }
    //MARK:懒加载
    private lazy var composeBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        btn.addTarget(self, action: #selector(MainViewController.composeBtnClick), for: UIControlEvents.touchUpInside)
        return btn
    }()
    @objc private func composeBtnClick(){
        print("点击了")
    }

}
